/**
 * [ajaxGetData ajax请求页面数据的函数]
 * @param  {[type]} data [对象]
 * @return {[type]}      [description]
 */
	
function ajaxGetData(data) //{url:inddd.jlll}
{
	var defaultData = {
		url  : 'look.html',
		data : {page:1},
		dataType:'json',
		element : '.number-table',
		dataType:'html',
		type:'get',
	},
	setting;
	setting = $.extend(defaultData,data);

	$.ajax({
		url  : setting.url,
		data : setting.data,
		type : setting.type,
		dataType :setting.dataType,
		async : false,
		success:function(data){
			// console.log(data);
			$(setting.element).html(data);
		}
	})
}

//点击搜索刷新页面,url传入搜索的值
$('.a-zhaoren').click(function(){
	var val = $('.input-zhaoren').val();
	$(this).attr('href','list.html?content='+val+'');
	// alert($(this).attr('href'));
});

//点击收藏按钮
$('.shaoC').click(function(){
	var index = $(this).parent().index();
	var owner_id = $(this).attr('data-id');
	var content = $(this).html();
	if(content == '收藏'){
		//进行收藏
		$.ajax({
			url:'shoucang.html',
			type:'get',
			dataType:'json',
			data:{'owner_id':owner_id,'type':1},
			success:function(data){
				if(data.code == 200){
					$('.shaoC').eq(index).html('取消收藏');	
					$('.shaoC').eq(index).removeClass('back87C743');
					$('.shaoC').eq(index).addClass('back999');
				}
			}
		});
	}else{
		//取消收藏
		$.ajax({
			url:'shoucang.html',
			type:'get',
			dataType:'json',
			data:{'owner_id':owner_id,'type':0},
			success:function(data){
				if(data.code == 200){
					$('.shaoC').eq(index).html('收藏');	
					$('.shaoC').eq(index).removeClass('back999');
					$('.shaoC').eq(index).addClass('back87C743');
				}
			}
		});
	}
});

//人选详情页面点击收藏按钮
$('.shaoC1').click(function(){
	var owner_id = $(this).attr('data-id');
	var content = $(this).html();
	if(content == '收藏'){
		//进行收藏
		$.ajax({
			url:'shoucang.html',
			type:'get',
			dataType:'json',
			data:{'owner_id':owner_id,'type':1},
			success:function(data){
				if(data.code == 200){
					$('.shaoC1').html('取消收藏');
					$('.shaoC1').removeClass('back87C743');
					$('.shaoC1').addClass('back999');	
				}
			}
		});
	}else{
		//取消收藏
		$.ajax({
			url:'shoucang.html',
			type:'get',
			dataType:'json',
			data:{'owner_id':owner_id,'type':0},
			success:function(data){
				if(data.code == 200){
					$('.shaoC1').html('收藏');	
					$('.shaoC1').removeClass('back999');
					$('.shaoC1').addClass('back87C743');
				}
			}
		});
	}
});

//点击推荐职位
$('.tuiJ').click(function(event){
	var owner_id = $(this).attr('data-id');
	var has_recommend_authority = $('.has_recommend_authority').val();
	var e = window.event || event;
	// var head_id = $(this).attr('head_id');
	if(has_recommend_authority == 1){	
		$.ajax({
			url:'tuijian.html',
			type:'get',
			dataType:'json',
			data:{'owner_id':owner_id},
			success:function(data){
				if(data.code == 400){
					$('.renzheng').css('display','block');
				};
				if(data.code == 200){
					// window.location.href = 'tjdetails.html?owner_id='+owner_id;
					window.open('tjdetails.html?owner_id='+owner_id);
				}
			}
		})
	}else{
		if(e.preventDefault){
			e.preventDefault();
		}else{
			e.returnValue = false;
		};
		alert('对不起,您暂时没有推荐权限');
	}
});


sessionStorage.setItem('page',0);
sessionStorage.setItem('group_id',0);
$('.addcolor').eq(0).css('color','#53B2FB');
$('.addcolor1').eq(0).css('color','#53B2FB');
var selected_job = [],
	selected_class     = 'back999',  //选中的样式back999
    not_selected_class = 'back87C743';  //未选中的样式
//进入页面查询默认信息
ajaxGetData({
	url  : 'joblist.html',
	data : {'id':0,'type':0},
	dataType:'html',
	element : '.zhiweidingxiang',
	dataType:'html',
});
//点击定向职位查询对应信息
$('.dingxiangzhiwei').click(function(){
	//得到所有已经选择的职位id
	selected_job = [];
    $.each($('.selected_job .remove'),function(event){
        selected_job.push($(this).attr('id'));
    })
    // console.log(selected_job);
	sessionStorage.setItem('group_id',0);
	var page = sessionStorage.getItem('page');
	// alert(111);
	ajaxGetData({
		url  : 'joblist.html',
		data : {'id':0,'type':0},
		dataType:'html',
		element : '.zhiweidingxiang',
		dataType:'html',
	});
	var obj1 = $('.choose_btn').filter(function(index){
					return $.inArray($(this).attr('data-id'),selected_job)==-1?false:true;
				});
	// console.log(obj1);
	obj1.removeClass(not_selected_class).addClass(selected_class).html('取消');
});
//点击收藏职位查询对应信息
$('.shoucangzhiwei').click(function(){
	//得到所有已经选择的职位id
	selected_job = [];
    $.each($('.selected_job .remove'),function(event){
        selected_job.push($(this).attr('id'));
    })
	sessionStorage.setItem('group_id',0);
	var page = sessionStorage.getItem('page');
	// alert(111);
	ajaxGetData({
		url  : 'joblist.html',
		data : {'id':0,'type':3},
		dataType:'html',
		element : '.zhiweishoucang',
		dataType:'html',
	});
	var obj1 = $('.choose_btn').filter(function(index){
					return $.inArray($(this).attr('data-id'),selected_job)==-1?false:true;
				});
	// console.log(obj1);
	obj1.removeClass(not_selected_class).addClass(selected_class).html('取消');
});

//点击定向职位下面的分页页码
$('.zhiweidingxiang').delegate('.pagination a','click',function(event){
	//得到所有已经选择的职位id
	selected_job = [];
    $.each($('.selected_job .remove'),function(event){
        selected_job.push($(this).attr('id'));
    })
	//阻止默认事件
	var e = window.event || event;
	var	page = $(this).data('page');

	if(e.preventDefault){
		e.preventDefault();
	}else{
		e.returnValue = false;
	};
	sessionStorage.setItem("page", page);
	var group_id = sessionStorage.getItem('group_id');
	ajaxGetData({
		url  : 'joblist.html',
		data : {'id':group_id,'type':0,'page':page+1},
		dataType:'html',
		element : '.zhiweidingxiang',
		dataType:'html',
	});
	var obj1 = $('.choose_btn').filter(function(index){
					return $.inArray($(this).attr('data-id'),selected_job)==-1?false:true;
				});
	// console.log(obj1);
	obj1.removeClass(not_selected_class).addClass(selected_class).html('取消');
})


//点击收藏职位下面的分页页码
$('.zhiweishoucang').delegate('.pagination a','click',function(event){
	//得到所有已经选择的职位id
	selected_job = [];
    $.each($('.selected_job .remove'),function(event){
        selected_job.push($(this).attr('id'));
    })
	//阻止默认事件
	var e = window.event || event;
	var	page = $(this).data('page');

	if(e.preventDefault){
		e.preventDefault();
	}else{
		e.returnValue = false;
	};
	sessionStorage.setItem("page", page);
	var group_id = sessionStorage.getItem('group_id');
	ajaxGetData({
		url  : 'joblist.html',
		data : {'id':group_id,'type':3,'page':page+1},
		dataType:'html',
		element : '.zhiweishoucang',
		dataType:'html',
	});
	var obj1 = $('.choose_btn').filter(function(index){
					return $.inArray($(this).attr('data-id'),selected_job)==-1?false:true;
				});
	// console.log(obj1);
	obj1.removeClass(not_selected_class).addClass(selected_class).html('取消');
})

//点击定向职位分组
$('.addcolor').click(function(){
	//得到所有已经选择的职位id
	selected_job = [];
    $.each($('.selected_job .remove'),function(event){
        selected_job.push($(this).attr('id'));
    })
	$('.addcolor').css('color','');
	$(this).css('color','#53B2FB');
	var id = $(this).attr('data-id');
	sessionStorage.setItem('page',0);
	sessionStorage.setItem('group_id',id);
	ajaxGetData({
		url  : 'joblist.html',
		data : {'id':id,'type':0},
		dataType:'html',
		element : '.zhiweidingxiang',
		dataType:'html',
	});
	var obj1 = $('.choose_btn').filter(function(index){
					return $.inArray($(this).attr('data-id'),selected_job)==-1?false:true;
				});
	// console.log(obj1);
	obj1.removeClass(not_selected_class).addClass(selected_class).html('取消');
});

//点击收藏职位分组
$('.addcolor1').click(function(){
	//得到所有已经选择的职位id
	selected_job = [];
    $.each($('.selected_job .remove'),function(event){
        selected_job.push($(this).attr('id'));
    })
	$('.addcolor1').css('color','');
	$(this).css('color','#53B2FB');
	var id = $(this).attr('data-id');
	sessionStorage.setItem('page',0);
	sessionStorage.setItem('group_id',id);
	ajaxGetData({
		url  : 'joblist.html',
		data : {'id':id,'type':3},
		dataType:'html',
		element : '.zhiweishoucang',
		dataType:'html',
	});
	var obj1 = $('.choose_btn').filter(function(index){
					return $.inArray($(this).attr('data-id'),selected_job)==-1?false:true;
				});
	// console.log(obj1);
	obj1.removeClass(not_selected_class).addClass(selected_class).html('取消');
});

//文本域必填js
$('.service').blur(function(){
	var service = $(this).val();
	if(service == ''){
		$(this).nextAll(".warning").removeClass('hide').show();
		$(this).addClass("border-orange");
	}else{
		$(this).nextAll(".warning").addClass('hide').hide();
		$(this).removeClass("border-orange");
	}
});

//邀请页面点击确认邀请
$('.invitionsure').click(function(event){
	// 阻止默认事件
	var e = window.event || event;
	if(e.preventDefault){
		e.preventDefault();
	}else{
		e.returnValue = false;
	};
	var url = $(this).attr('href');
	var service = $('.service').val();
	if(service == ''){
		$('.service').nextAll(".warning").removeClass('hide').show();
		$('.service').addClass("border-orange");
	}else{
		$('.service').nextAll(".warning").addClass('hide').hide();
		$('.service').removeClass("border-orange");
		location.href = url+'&content='+service;
	}
	

});

//页面感叹号移入移出效果
$('.tuijianshu').mouseover(function(){
	var index = $(this).parent().index();
	// alert(index);
	// $('.sigh-alert').css('display','none');
	$('.sigh-alert').eq(index).css('display','block');
});
$('.tuijianshu').mouseout(function(){
	var index = $(this).parent().index();
	$('.sigh-alert').css('display','none');
	// $('.sign-alert').eq(index).css('display','block');
});



(function(){
	var choosed_num = 0,
        max_choosed_num = 3,  //最大选择数
        selected_class     = 'back999',  //选中的样式back999
        not_selected_class = 'back87C743';  //未选中的样式
        recmd_job_url = "<?= \yii\helpers\Url::to(['/owner/default/get_coll_job'])?>";

       //添加和删除的操作函数  用于下方的收藏职位的选择和取消的操作
        function operate($type,obj)
        {
            var $li = obj.closest('li'),
                job_id = obj.data('id'),
                job_name = $li.find('.job_name').html(),
                work_exp = $li.find('.work_exp').html(),
                m_p = $li.find('.month_pay').html(),
                edu = $li.find('.edu_require').html(),
                url = $li.find('a').eq(0).attr('href');
                html = '';
        
            if($type == 0){

                //取消
                $('#'+job_id).closest('li').remove();
                $('.selected_num').html(choosed_num);
                
            }else if($type == 1){

                //选择
                $('.selected_num').html(choosed_num);

                html =  '<li>'+
		                    '<a href="'+web_url+'/home/job/job-detail.html?job_id='+job_id+'">'+job_name+'</a>'+
		                    '<p><span><span>'+m_p+'</span></span></p>'+
		                    '<p>'+edu+' / 经验'+work_exp+'</p>'+
		                   '<a id="'+job_id+'" class="remove" href="##">移除</a>'+
                        '</li>';
                // console.log(html);
                $('.selected_job').prepend(html);
                
            }

            //判断是否要显示确认推荐按钮
            if(choosed_num > 0){
                $('.selected_job>a').show();
            }else{
                 $('.selected_job>a').hide();
            }
        }


         function operate_fix(type,obj)
         {    
           head_id = obj.data('id');
           if(type ==1){

              //选择
              $.each($('.choose_btn'),function(i){
                 if($(this).data('id') == head_id ){
                    $(this).removeClass(not_selected_class).addClass(selected_class).html('取消');
                 }
              })
           }else{
              //取消
              $.each($('.choose_btn'),function(i){
                 if($(this).data('id') == head_id ){
                    $(this).removeClass(selected_class).addClass(not_selected_class).html('选择');
                 }
              })
           }
        }

		$('body').delegate('.choose_btn','click',function(event){
            var e = event || window.event;
             if(e.preventDefault){
                e.preventDefault();
            }else{
                e.returnValue = false;
            }

            // if(!settings.use_choose_btn){
            //     location.href = $(this).attr('href');
            //     return true;
            // }

            //查看该对象是否已经点击
            if($(this).hasClass(selected_class)){

                //点击取消
                 choosed_num-=1;
                 operate(0,$(this));
                 operate_fix(0,$(this));
                 
                 // console.log(choosed_num);
                
            }else{

                //点击选择
                if(choosed_num < max_choosed_num){
                    choosed_num+=1;
                    operate(1,$(this));
                    operate_fix(1,$(this));
                    // console.log(choosed_num);
                }else{
                	alert('最多选择'+max_choosed_num+'个职位');
                }
               
            }
        }).delegate('.remove','click',function(event){
            var head_id = $(this).attr('id');

            // if(!settings.use_choose_btn){
            //     return true;
            // }

            //移除自身的职位
            $(this).closest('li').remove();
            choosed_num-=1;
            $('.selected_num').html(choosed_num);


             //判断是否要显示确认推荐按钮
            if(choosed_num > 0){

                $('.selected_job>a').show();
            }else{
                 $('.selected_job>a').hide();
            }

            //移除下方的收藏职位的选中样式
            $.each($('.choose_btn'),function(){
                if($(this).data('id') == head_id){
                    $(this).removeClass(selected_class).addClass(not_selected_class).html('选择');
                }
            })
        })



        $('.selected_job>a').click(function(event){
            var e = event || window.event,
                selected_job = [];
                // job_id = $(this).closest('div').find('input[name="job_id"]').val();
            if(e.preventDefault){
                e.preventDefault();
            }else{
                e.returnValue = false;
            }

            //得到所有已经选择的职位id
            $.each($('.selected_job .remove'),function(event){
                selected_job.push($(this).attr('id'));
            })
    		
            location.href = $(this).attr('href')+'&job_id='+selected_job.join('-');
        })
}())


 